package vcp.server;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
 * 
 * @author VCP team
 *<p> data base adapter class </p>
 */
public class DBAdapter
{
	private static DBAdapter adapter;
	/**
	 * gets adapter
	 * @return adapter instance
	 */
	public static synchronized DBAdapter getAdapter ()
	{
		if ( DBAdapter.adapter == null )
		{
			DBAdapter.adapter = new DBAdapter();
			DBAdapter.adapter.startDB();
		}
		return DBAdapter.adapter;
	}

	private Connection connection;
	/**
	 * gets connection
	 * @return connectio instance
	 */
	public Connection getConnection ()
	{
		return this.connection;
	}
	/**
	 * class constructor
	 */
	private DBAdapter ()
	{
	}
	/**
	 * starts up data base
	 */
	public void startDB ()
	{
		if ( this.connection == null )
		{
			try
			{
				Class.forName("com.mysql.jdbc.Driver").newInstance();
				this.connection = DriverManager.getConnection("jdbc:mysql://localhost/vcp", "root", "Braude");
				System.out.println("DB is running");
			}
			catch ( final Exception e )
			{
				e.printStackTrace();
				System.out.println("cannot connect DB");
			}
		}
	}
	/**
	 * stops connection to data base
	 */
	public void stopDB ()
	{
		try
		{
			if ( this.connection != null )
			{
				this.connection.close();
				System.out.println("DB is closed");
			}
		}
		catch ( final SQLException e )
		{
			e.printStackTrace();
		}
		finally
		{
			this.connection = null;
		}
	}
	/**
	 * saves data into DB
	 * @param sqlQuery
	 * @return true if save success
	 * @throws SQLException
	 */
	public boolean save ( String sqlQuery ) throws SQLException
	{
		Boolean success = null;
		try
		{
			final Statement statement = this.connection.createStatement();
			statement.execute(sqlQuery);
			success = new Boolean(true);
		}
		catch ( final SQLException e )
		{
			e.printStackTrace();
			success = new Boolean(false);
		}

		return success.booleanValue();
	}
	/**
	 * refreshed table in DB once updated single field
	 * @param sqlQuery
	 * @return true id update success
	 * @throws SQLException
	 */
	public boolean refresh ( String sqlQuery ) throws SQLException
	{
		boolean success = true;
		try
		{
			final Statement statement = this.connection.createStatement();
			statement.executeUpdate(sqlQuery);
		}
		catch ( final SQLException e )
		{
			e.printStackTrace();
			success = ! success;
		}

		return success;
	}
	/**
	 * deletes data from table
	 * @param sqlQuery
	 * @return true if delete success
	 * @throws SQLException
	 */
	public boolean delete ( String sqlQuery ) throws SQLException
	{
		return this.refresh(sqlQuery);
	}
	/**
	 * finds data in table 
	 * @param sqlQuery
	 * @return result set with relevant data
	 * @throws SQLException
	 */
	public ResultSet find ( String sqlQuery ) throws SQLException
	{
		ResultSet answers = null;

		try
		{
			final Statement statement = this.connection.createStatement();
			answers = statement.executeQuery(sqlQuery);
		}
		catch ( final SQLException e )
		{
			e.printStackTrace();
		}

		return answers;
	}
}
